{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Output Widget\n",
    "\n",
    "Stdout and stderr are normally directed to appear in the output of the cell whose code produced them.  The output widget allows you to consolidate output in one place, both captured IO and rich objects.\n",
    "\n",
    "This can be used for logging and also for catching the output of background threads, which can be left running after the cell that starts them completes.\n",
    "\n",
    "The widget can be configured to capture just stdout or just stderr.\n",
    "\n",
    "## Example\n",
    "\n",
    "First we create the Output widget, and direct output to it.\n",
    "The `setOutput` method returns the same widget you pass to it, so the widget is returned from the cell.\n",
    "Initially there is nothing in it, the Output is empty and so nothing appears."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%import com.twosigma.beakerx.widget.Output\n",
    "out = new Output()\n",
    "OutputManager.setOutput(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next cell starts a background thread that writes into that output.  The results appear above, in the output widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = new Thread({\n",
    "    println(\"background thread start\")\n",
    "    for (i = 0; i < 20 ; i++) {\n",
    "        Thread.sleep(1000)\n",
    "        println(\"tick \" + i)\n",
    "        if (i % 5 == 3) System.err.println(\"stderr!\")\n",
    "        if (i % 6 == 2) display(HTML('any <b>MIME</b> <em>type</em>'))\n",
    "        if (i % 7 == 4) out.display(new Plot(initHeight:150) << new Line(y: [0,5,2,3,11]))    \n",
    "    }});\n",
    "println(\"ready set go\")\n",
    "t.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the following cells after starting the threads with the previous cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "println(\"some other cell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "display(HTML('any <b>MIME</b> <em>type</em>'))\n",
    "display(new Plot(initHeight:150) << new Line(y: [0,11,2,8,1]))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.clearOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.setOutput(null)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Only Standard Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.setStandardOutput(new Output())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "println(\"only stdout is captured\")\n",
    "System.err.println(\"stderr goes to its own cell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.clear()\n",
    "OutputManager.setStandardOutput(null)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Only Standard Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.setStandardError(new Output())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "println(\"stdout goes to its own cell\")\n",
    "System.err.println(\"only stderr is captured\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OutputManager.clear()\n",
    "OutputManager.setStandardError(null)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
